<?php

/**
 * 行为分析-每天分类PV汇总数据
 *
 * @author meng.deng
 */

require_once 'CommonController.php';

class Admin_ActDayDataCategoryController extends Admin_CommonController
{
    /**
     * 初始化函数
     * 
     * @return void
     */
    public function init()
    {
        parent::init();
        $this->_model = new Application_Model_ActDayDataCategory();
        $this->view->title = "管理中心";
    }

    /**
     * 默认动作
     * 
     * @return void
     */
    public function indexAction()
    {
        $time = time();
        $cookieModel = new Application_Model_Cookie();

        //获取当前页码
        $pageSize = 20;
        $pageNo = intval($this->_request->get('pageNo'));
        if (empty($pageNo)) {
            $pageNo = 1;
        }

        //设置URL模板以及条件
        $urls = array();
        $vars = array();
        $wheres = array();

        $vars['osName'] = trim($this->_request->get('osName'));
        $vars['version'] = trim($this->_request->get('version'));
        $vars['category'] = trim($this->_request->get('category'));
        $vars['startTime'] = trim($this->_request->get('startTime'));
        $vars['endTime'] = trim($this->_request->get('endTime'));
        $urls = $vars;

        $wheres[] = "`productCode` = '". Application_Model_UserLogin::getInstance()->getUserCurrentProductCode() ."'";
        if ($vars['osName'] != '') {
            $wheres[] = "`osName` = '" . addslashes($vars['osName']) . "'";
        }
        if ($vars['version'] != '') {
            $wheres[] = "`version` = '" . addslashes($vars['version']) . "'";
        }
        if ($vars['category'] != '') {
            $wheres[] = "`category` like '%" . addslashes($vars['category']) . "%'";
        }
        if ($vars['startTime'] != '') {
            $wheres[] = "`date` >= '{$vars['startTime']}' ";
        }
        if ($vars['endTime'] != '') {
            $wheres[] = "`date` <= '{$vars['endTime']}' ";
        }
        $wheres[] ="`date`>='2011-09-24' and `category`<>'分类浏览'";
        $orderBy = "`date` DESC,`pv` DESC";

        $urls['pageNo'] = '{page}';
        $urlTemplate = $this->view->projectUrl($urls);

        //获取总页数以及记录
        $where = count($wheres) > 0 ? ' WHERE ' . implode(' AND ', $wheres) . ' ' : '';
        $sql = "SELECT * FROM `{$this->_model->getTableName()}` {$where} ORDER BY {$orderBy}";

        $dbPaginator = new Application_Model_DbPaginator($sql, $pageSize, $pageNo);
        $recordCount = $dbPaginator->getRecodCount();

        //获取分页html字符
        Zend_Loader::loadClass('Project_Paginator');
        $paginator = new Project_Paginator($recordCount, $pageSize, $pageNo);
        $paginator->urlTemplateContent = $urlTemplate;

        $rows = $dbPaginator->getRows();

        //分配view(分页处理后的记录集以及分页HTML字符)
        $this->view->pageString = $paginator->getPageString();
        $this->view->rows = $rows;
        $urls['pageNo'] = $pageNo;
        $this->view->pageUrl = $this->view->projectUrl($urls);
        $this->view->vars = $vars;
        
        /*
        //获取操作系统及版本号
        $osNameAndVersionRows = $this ->_model ->getAdapter()->fetchAll("SELECT `osName`,`version` FROM `{$this->_model->getTableName()}` GROUP BY `osName`,`version` ORDER BY `osName`");
        $osNameArr = array();
        $appInfoList = array();
        $keyFlag = 0;
        //重新组装数组
        foreach ($osNameAndVersionRows as $key =>$row) {
            if (!in_array($row['osName'],$osNameArr)){
                $osNameArr[] = $row['osName'];
                $appInfoList[$key]['osName'] = $row['osName'];
                $keyFlag = $key;
            }
            $appInfoList[$keyFlag]['version'][] = $row['version'];
        }
        //print_r($appInfoList);
        */
        
        //列出操作系统
        $osNameRows = $this ->_model ->getAdapter()->fetchAll("SELECT `osName` FROM `{$this->_model->getTableName()}` GROUP BY `osName` ORDER BY `osName`");
        $this->view->osNames = $osNameRows;
        
    }
    
    /**
     * 获取APP版本号
     * @return json string
     */
    public function appversionAction() 
    {
        $osName = trim($this->_request->getPost('osName'));
        $rows = $this ->_model ->getAdapter()->fetchAll("SELECT `version` FROM `{$this->_model->getTableName()}` WHERE `osName`='".$osName."' GROUP BY `version` ORDER BY `version`");
        $res = array();
        foreach($rows as $row) {
            $res[]=$row['version'];
        }
        $json_string=json_encode($res);
        echo $json_string;
        exit();
        
    }
    
    
    
    
}